package com.jy.appmarket.screenlock.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;

import com.jy.app.market.idata.Doc;
import com.jy.app.market.idata.data.LockscreenCfg;
import com.jy.appmarket.utils.LogUtil;

/**
 * 类说明：
 * 
 * @author xinhui.cheng
 * @date 2015-2-13
 * @version 1.0
 */
public class LockScreenConfig {
	private static final String TAG = LockScreenConfig.class.getSimpleName();
	private static SharedPreferences sSp;
	private static final String FILE_NAME = "lockScreenConfig";

	private static final String WALLPAPER_OF_WIFI = "wallpaperOfWifi";
	// 锁屏界面，消息开关
	private static final String MESSAGE_CHANGE = "message_change";
	// 锁屏消息拦截的包名
	private static final String CUSTOM_MESSAGE_PGK = "custom_message_pkg";

	private LockScreenConfig() {
	}

	private synchronized static SharedPreferences getInstance(Context context) {
		if (sSp == null) {
			sSp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		}
		return sSp;
	}

	/**
	 * 获取是否在WIFI下开启壁纸
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWallpaperOfWifi(Context context) {
		SharedPreferences sp = getInstance(context);
		return sp.getBoolean(WALLPAPER_OF_WIFI, true);
	}

	public static void setWallpaperOfWifi(Context context, boolean isUse) {
		SharedPreferences sp = getInstance(context);
		sp.edit().putBoolean(WALLPAPER_OF_WIFI, isUse).commit();
	}

	public static void setMessageTip(Context context, boolean change) {
		SharedPreferences sp = getInstance(context);
		sp.edit().putBoolean(MESSAGE_CHANGE, change).commit();
	}

	public static boolean isMessageTip(Context context) {
		SharedPreferences sp = getInstance(context);
		return sp.getBoolean(MESSAGE_CHANGE, true);
	}

	public static Set<String> getCustomMessagePkg(Context context) {
		SharedPreferences sp = getInstance(context);
		return sp.getStringSet(CUSTOM_MESSAGE_PGK, null);
	}

	public static void setCustomMessagePkg(Context context, Set<String> pkg) {
		SharedPreferences sp = getInstance(context);
		sp.edit().putStringSet(CUSTOM_MESSAGE_PGK, pkg).commit();
	}

	public static boolean setLockscreenConfig(Context context, LockscreenCfg config) {
		File file = new File(context.getFilesDir(), "lock_screen");
		if (config == null || config.getConfig() == null || config.getConfig().isEmpty()) {
			file.delete();
			return true;
		}

		FileOutputStream fos = null;
		ObjectOutputStream oos = null;

		try {
			fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(config);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
				fos.close();
			} catch (Exception e) {
				// ignore
			}
		}
		return false;
	}

	public static LockscreenCfg getLockscreenConfig(Context context) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		File file = new File(context.getFilesDir(), "lock_screen");
		if (!file.exists()) {
			return null;
		}
		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			Object obj = ois.readObject();
			if (obj == null || !(obj instanceof LockscreenCfg)) {
				return null;
			}
			return (LockscreenCfg) obj;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			LogUtil.e(TAG, "read object failed");
			file.delete();
		} catch (Exception e) {
			LogUtil.e(TAG, "unknown error:" + e.getMessage());
		} finally {
			try {
				ois.close();
				fis.close();
			} catch (Exception e) {
				// ignore
			}
		}
		return null;
	}
	
	public static LockscreenCfg getDefLockscreenConfig(Context context) {
		InputStream is = null;
		
		try {
			is = context.getResources().getAssets().open("def_lock_screen");
			byte[] bytes = new byte[1024 * 10];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			while ((len = is.read(bytes)) != -1) {
				baos.write(bytes, 0, len);
			}
			String result = new String(baos.toByteArray(), "UTF-8");
			Doc doc = Doc.fromJson(result);
			if (doc == null) {
				return null;
			}
			if (doc.isOk()) {
				if (doc.isType(LockscreenCfg.class)) {
					return doc.getData();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			LogUtil.e(TAG, "unknown error:" + e.getMessage());
		} finally {
			try {
				if(is != null) {
					is.close();
				}
			} catch (Exception e) {
				// ignore
			}
		}
		return null;
	}
}
